# -*- coding:utf-8 -*-
import os
import math
import unittest

class FSInfo:
    def __init__(self):
        self.mem_dir = "/dev/shm"
        self.receive_cache_dir = "../cache"
        self.send_backup_dir = "../backup"
        self.mem_size = 0
        self.mem_used = 0
        self.cache_hd_size = 0
        self.cache_hd_used = 0
        self.backup_hd_size = 0
        self.backup_hd_used = 0
    
    def set_receive_cache_dir(self, receiveCacheDir):
        """
        设置缓存目录
        """
        self.receive_cache_dir = receiveCacheDir
        
    def set_send_backup_dir(self, sendCacheDir):
        """
        设置备份目录
        """
        self.send_backup_dir = sendCacheDir
        
    def set_receive_dir(self, receive_dir):
        """
        设置接收目录
        """
        tmp_dir = receive_dir.rstrip("/")
        par_receive_dir = os.path.dirname(tmp_dir)
        self.mem_dir = par_receive_dir
        
    def update_cache_hd_info(self):
        """
        更新缓存硬盘信息
        """
        st = os.statvfs(self.receive_cache_dir)
        self.cache_hd_used = int(math.ceil(float(st.f_blocks - st.f_bfree) / float(st.f_blocks - st.f_bfree + st.f_bavail) * 100))
        self.cache_hd_size = st.f_bsize * st.f_blocks / 1024 / 1024 / 1024
                
    def update_backup_hd_info(self):
        """
        更新备份硬盘信息
        """
        st = os.statvfs(self.send_backup_dir)
        self.backup_hd_used = int(math.ceil(float(st.f_blocks - st.f_bfree) / float(st.f_blocks - st.f_bfree + st.f_bavail) * 100))
        self.backup_hd_size = st.f_bsize * st.f_blocks / 1024 / 1024 / 1024
    
    def update_mem_info(self):
        """
        更新内存盘信息
        """
        st = os.statvfs(self.mem_dir)
        self.mem_used = int(math.ceil(float(st.f_blocks - st.f_bfree) / float(st.f_blocks - st.f_bfree + st.f_bavail) * 100))
        self.mem_size = st.f_bsize * st.f_blocks / 1024 / 1024
    
    def update_info(self):
        """
        更新所有信息
        """
        self.update_cache_hd_info()
        self.update_backup_hd_info()
        self.update_mem_info()
        
    
    def show(self):
        print "CacheHDInfo:"
        print "\tSize:\t", self.cache_hd_size, "G"
        print "\tUsed:\t", self.cache_hd_used, "%"
        print "BackupHDInfo:"
        print "\tSize:\t", self.backup_hd_size, "G"
        print "\tUsed:\t", self.backup_hd_used, "%"
        print "MemInfo:"
        print "\tSize:\t", self.mem_size, "M"
        print "\tUsed:\t", self.mem_used, "%"
    
class FSInfoTestCase(unittest.TestCase):
    def setUp(self):
        self.fsinfo = FSInfo() 
    
    def tearDown(self):
        pass
    
    def testGetFileSystemInfo(self):
        self.fsinfo.UpdateInfo()
        self.fsinfo.PrintInfo()
        
if __name__ == "__main__":
    suite = unittest.TestSuite()
    suite.addTest(FSInfoTestCase("testGetFileSystemInfo"))
    
    runner = unittest.TextTestRunner()
    runner.run(suite)
